var express = require('express');
var md5 = require('md5');
var request = require('request');
var consts = require('../common/constants');

var router = express.Router();

//获取省市区接口
router.get('/province', function(req, res, next) {
    var params = {
        pcode: req.query.pcode
    };

    var token = JSON.stringify(params) + "|" + consts.MD5_String;
    token = md5(token);
    var url = consts.APP_HOST + 'address/areas.htm?token=' + token;
    var options = {
        url: url,
        method: 'POST',
        json: true,
        body: params
    };

    request(options, function(err, httpResponse, body) {
        if (1 == body.MobileHead.Code) {
            res.send(body.MobileBody);
        } else {
            res.send('404');
        }
    });
});

//获取省市区接口 获取级联全部
router.get('/all', function(req, res, next) {
    var params = {};
    var token = JSON.stringify(params) + "|" + consts.MD5_String;
    token = md5(token);
    var url = consts.APP_HOST + 'address/allareas.htm?token=' + token;
    var options = {
        url: url,
        method: 'POST',
        json: true,
        body: params
    };

    request(options, function(err, httpResponse, body) {
        if (1 == body.MobileHead.Code) {

           res.send(body.MobileBody);

          
        } else {

           res.send('404');

          
        }


    });
});

module.exports = router;